/*
 * @Description:
 * @version:
 * @Author: 尹鹏孝
 * @Date: 2023-10-25 21:35:04
 * @LastEditors: 尹鹏孝
 * @LastEditTime: 2023-10-28 10:10:34
 */
import { Body,Query, Controller, Get, Post, Req, Res, Param, Module,Put, Delete } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { UseGuards } from '@nestjs/common'
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
import { User } from 'src/entity/user.entity';
import {LoginService} from '../login/login.service'
import { UserDetail } from 'src/entity/userDetail.entity';
import { PageInit,PageNation } from 'src/interface/page.interface';
import { Departments } from './../entity/departments.entity';
import { DepartmentsService } from './departments.service';
@Controller('departments')
export class DepartmentsController {

      constructor(private  departmentsRepository: DepartmentsService) {

      }
      /**
     *
     * @param user :用户名称 username:"用户名"
     * @returns
     */
    @UseGuards(JwtAuthGuard)
    @Post('create')
    async create(@Body() departments: Departments) {
        try {

            let data =await  this.departmentsRepository.findBy(departments)
            // console.log('qingq:',data)
            if (data.length===0) {
                  let res = await this.departmentsRepository.create(departments)
                console.log(res)
                 return {
                code: 200,
                msg:'请求成功'
                }
            }else {
                  return {
                    code:400,
                    msg:'已经存在请不要重复创建'
                }
            }

        } catch (error) {
            console.log(error);
            return {
                code: 500,
                msg:error
            }
        }
    }
    @UseGuards(JwtAuthGuard)
    @Put('update')
    async update(@Body() departments: Departments) {
        try {
            let one = await this.departmentsRepository.findById(departments);
            if (one.length) {
                let user = await this.departmentsRepository.findBy(departments)
                console.log('输出用户',user)
                if (user.length) {
                    let data = await this.departmentsRepository.update(departments, departments.id);
                    if (data.affected) {
                        return {
                               code: 200,
                                msg:'更新成功'
                          };
                    } else {
                        return {
                                code: 400,
                                 msg:'参数错误，更新失败'
                        }
                    }

                } else {
                    return {
                        code: 400,
                        msg:'不存在此用户，无法更新'
                    }
                }


            } else {
                return {
                    code: 400,
                    msg:'不存在此机构id，无法更新'
                }
            }

        } catch (error) {
            return {
                code: 500,
                msg:error
            }
        }


    }


     @UseGuards(JwtAuthGuard)
    @Get('get')
    async get(@Query() query) {
        try {
            // let one = await this.departmentsRepository.findById(query);
            console.log(query)
            const res = await this.departmentsRepository.pageQuery(query);
            return res;
        } catch (error) {
            return {
                code: 500,
                msg:error
            }
        }


    }
    @UseGuards(JwtAuthGuard)
    @Delete('delete')
    async delete(@Query() query) {
        console.log(query)
        let idNew = Number(query.id);
        console.log(idNew)
        if (typeof idNew === undefined) {
             return {
                code: 400,
                msg:'参数不能为空'
            }
        }else if ( isNaN(idNew)) {
             return {
                code: 400,
                msg:'参数为数字'
            }
        }
        try {
           let one= await this.departmentsRepository.findById({
               id:idNew,
               name: '',
               userId: 0,
               createName: '',
               deletedTime: null
           })
            console.log('11:',one)
        if (one.length) {
            let res = await this.departmentsRepository.delete(idNew)
            if (res.affected) {
                return {
                code: 200,
                msg:'删除成功'
            }
            }
        } else {
             return {
                code: 400,
                msg:'删除失败'
            }
        }
        } catch (error) {
             return {
                code: 500,
                msg:error
            }
        }

    }

}
